package demo.test;

import java.awt.AlphaComposite;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
 * http://www.java.net/forum/topic/javadesktop/java-desktop-technologies/swing-awt/drawing-selection-box-using-swing-1
 * @author 250439
 *
 */
public class zaznacz extends JPanel implements MouseListener,
		MouseMotionListener {

	Rectangle newLine = null;

	public zaznacz() {

		addMouseListener(this);
		addMouseMotionListener(this);

	}

	static class Rectangle {

		int x1, y1, x2, y2;

		Rectangle(int _x1, int _y1, int _x2, int _y2) {

			x1 = _x1;
			y1 = _y1;
			x2 = _x2;
			y2 = _y2;
		}

		void paint(Graphics g) {

			g.drawRect(x1, y1, x2, y2);

		}

	}

	public void mouseClicked(MouseEvent e) {

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		startPoint = e.getPoint();
		setOpaque(true);

		Graphics2D g2 = (Graphics2D) getGraphics();

		Rectangle2D prostokat = new Rectangle2D.Double();
		prostokat.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,
				startPoint.x, startPoint.y);
		g2.setComposite(AlphaComposite.getInstance(rule, alpha));
		g2.draw(prostokat);
		g2.setColor(Color.BLUE);
		g2.fill(prostokat);

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		repaint();

	}

	Point startPoint;

	@Override
	public void mouseDragged(MouseEvent e) {
		setOpaque(true);

		Graphics2D g2 = (Graphics2D) getGraphics();
		Rectangle2D prostokat = new Rectangle2D.Double();

		prostokat.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,
				startPoint.x, startPoint.y);
		g2.setComposite(AlphaComposite.getInstance(rule, alpha));
		g2.draw(prostokat);
		g2.setColor(Color.BLUE);
		g2.fill(prostokat);
		paintComponent(g2);

	}

	@Override
	public void mouseMoved(MouseEvent e) {

	}

	int rule = AlphaComposite.SRC_OVER;
	float alpha = 0.85F;

	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				zaznacz rys = new zaznacz();
				JFrame frame = new JFrame();
				JButton Button = new JButton("1");
				JPanel panel = new JPanel();

				panel.add(Button);
				rys.add(panel);
				frame.setSize(400, 300);
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				panel.setOpaque(false);

				frame.add(rys);

			}
		});
	}

}
